package com.ycl.web.backend;

import com.ycl.model.EbNews;
import com.ycl.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
     * 新闻分页查询
     * @param model
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public String newsList(Model model,
                           @RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        List<EbNews> ebNews = newsService.list(page, pageSize);
        model.addAttribute("ebNews",ebNews);
        return "manage/news";
    }

    @RequestMapping("/to_save")
    public String toSave(){
        //spring
        return "manage/news-add";
    }

    @RequestMapping("/save")
    public String save(EbNews ebNews){
        Integer save = newsService.save(ebNews);
        if(save>0){
            return "manage/newssave-result";
        }else{
            return "manage/newssave-error";
        }
    }

    @RequestMapping("/to_edit")
    public String toEdit(Model model,
                         @RequestParam("enId")int enId){
        EbNews ebNews = newsService.selectOne(enId);
        model.addAttribute("ebNews",ebNews);
        return "manage/news-modify";
    }

    @RequestMapping("/edit")
    public String edit(Model model,EbNews ebNews) {
        System.out.println("ebNews>"+ebNews);
        Integer result = newsService.updateNews(ebNews);
        if (result > 0) {
            return "manage/manage-result";
        } else {
            return "manage/manage-error";
        }
    }

}
